home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / ProgValTest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.5 KB  |  57 lines

  1. //: C20:ProgValTest.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. //{L} ProgVals
  7. #include "ProgVals.h"
  8. using namespace std;
  9.  
  10. string defaults[][2] = {
  11.   { "color", "red" },
  12.   { "size", "medium" },
  13.   { "shape", "rectangular" },
  14.   { "action", "hopping"},
  15. };
  16.  
  17. const char* usage = "usage:\n"
  18. "ProgValTest [flag1=val1 flag2=val2 ...]\n"
  19. "(Note no space around '=')\n"
  20. "Where the flags can be any of: \n"
  21. "color, size, shape, action \n";
  22.  
  23. // So it can be used globally:
  24. ProgVals pvals(defaults, 
  25.   sizeof defaults / sizeof *defaults);
  26.  
  27. class Animal {
  28.   string color, size, shape, action;
  29. public:
  30.   Animal(string col, string sz, 
  31.     string shp, string act) 
  32.     :color(col),size(sz),shape(shp),action(act){}
  33.   // Default constructor uses program default
  34.   // values, possibly change on command line:
  35.   Animal() : color(pvals["color"]), 
  36.     size(pvals["size"]), shape(pvals["shape"]),
  37.     action(pvals["action"]) {}
  38.   void print() {
  39.     cout << "color = " << color << endl
  40.       << "size = " << size << endl
  41.       << "shape = " << shape << endl
  42.       << "action = " << action << endl;
  43.   }
  44.   // And of course pvals can be used anywhere
  45.   // else you'd like.
  46. };
  47.  
  48. int main(int argc, char* argv[]) {
  49.   // Initialize and parse command line values
  50.   // before any code that uses pvals is called:
  51.   pvals.parse(argc, argv, usage);
  52.   pvals.print();
  53.   Animal a;
  54.   cout << "Animal a values:" << endl;
  55.   a.print();
  56. } ///:~
  57.